<?php

/*
Plugin Name: Gravityforms Ml
Plugin URI: https://notabug.org/wp/gf-ml/
Description: This is going to have more limitation on gravityforms.
Version: 1.0
Author: saeb molaee
Author URI: https://twitter.com/sae13
License: GPL3
Text Domain: gf-ml
Domain Path: /languages
*/

if (!defined('ABSPATH')) {
    exit;
}
function __p($string)
{
    return __($string, 'gf-ml');
}

if (!class_exists('GFAPI')) {
    function gf_ml_admin_notice_gf_needed()
    {
        deactivate_plugins(plugin_basename(__file__));
        ?>
        <div class="notice notice-success is-dismissible">
            <p><?php echo esc_html(__p('GravityForms is not Installed please')); ?>
                <a href="https://github.com/wp-premium/gravityforms/tags"><?php echo esc_html(__p("download and install it")) ?></a>


            </p>
        </div>
        <?php
    }

    add_action('admin_notices', 'gf_ml_admin_notice_gf_needed');
}

if (!defined('gf_ml_slug')) {
    define('gf_ml_slug', 'gf_ml');
}

if (!defined('gf_ml_dir')) {
    define('gf_ml_dir', plugin_dir_path(__FILE__));
}

if (!defined('gf_ml_url')) {
    define('gf_ml_url', plugins_url('', __FILE__) . '/');
}
require_once gf_ml_dir.'includes/main_gf_ml.php';
//deactivate_plugins(plugin_basename(__file__));
if (!function_exists('err_log')) {
    function err_log($obj)
    {
        error_log(
            print_r($obj, true)
        );
    }
}
function gf_ml_activate_plugin(){
    require_once gf_ml_dir.'includes/gf_ml_wpdb.php';
    gf_ml_create_db();
}
function gf_ml_add_languages()
{
    load_plugin_textdomain('gf-ml',
        false,
        basename(dirname(__FILE__)) . '/languages');
}
function gf_ml_ajax()
{
    $response = array();

    if (isset($_POST['fieldid'])) {
        $fieldid = intval($_POST['fieldid']);
        $formid = intval($_POST['formid']);
//        err_log($formid);
//        err_log($fieldid);
        $field = GFAPI:: get_field($formid, $fieldid);
        $choices = $field['choices'];
        if (gettype($choices) == "array") {
            $index = 0;
            foreach ($choices as $choice) {
                $choice_array = array();
                $choice_array['index'] = $index++;
                $choice_array['text'] = $choice['text'];
                $choice_array['value'] = $choice['value'];
                array_push($response, $choice_array);

            }
        }
//
//            llog($response);
//
//        }
//
    } elseif (isset($_POST['formid'])) {
        $formid = intval($_POST['formid']);
        $form = GFAPI::get_form($formid);
        if (gettype($form) == "array") {
            $fields = $fields = GFAPI::get_fields_by_type($form,
                array('select', 'checkbox', 'radio'), true);
            foreach ($fields as $field) {
                if (strlen($field['label']) > 0) {
                    $field_array = array();
                    $field_array['value'] = $field['id'];
                    $field_array['text'] = $field['label'];
                    array_push($response, $field_array);
                }

            }
//            llog($response);

        }

    }

    header("Content-Type: application/json");
    echo json_encode($response);
    //Don't forget to always exit in the ajax function.
    exit();
}
function ajax_tranlation()
{
    $response = array();
    $response['status'] = 'ok';
    $response['float'] = (is_rtl() ? 'right' : 'left');
    if (isset($_POST['textdict'])) {
        $text_dict = $_POST['textdict'];
        if (gettype($text_dict) == "array") {
            foreach ($text_dict as $sentense) {
                $response[$sentense] = __p($sentense);
            }
        }
    }
    header("Content-Type: application/json");
    echo json_encode($response);
    exit();
}
function get_limits_ajax() {
    $response = array();
    $response['msg']=__p('saved');
    if (isset($_POST['limits_on'])){
        require_once gf_ml_dir."configs.php";
        global $wpdb;
        $table_name = gf_ml_tablename();
        $limits_on = $_POST['limits_on'];
        $form_id = intval($_POST['form_id']);
        $limit_number = intval($_POST['limit_number']);
        $u=wp_get_current_user()->ID;
        $status = $wpdb->insert(
                $table_name,
                array(
                        'form_id'=>$form_id,
                        'limit_number'=>$limit_number,
                        'limits_on'=>json_encode($limits_on),
                        'added_by'=>$u
                )
        );
        //err_log($limits_on);
        //err_log($status);
    }
    header("Content-Type: application/json");
    echo json_encode($response);
    //Don't forget to always exit in the ajax function.
    exit();
}

add_action('init', 'gf_ml_add_languages');
add_action('wp_ajax_formaction', 'gf_ml_ajax');
add_action('wp_ajax_sendingLimits', 'get_limits_ajax');
add_action('wp_ajax_translate', 'ajax_tranlation');
add_action('activate_plugin', 'gf_ml_activate_plugin');
//add_action('plugins_loaded','gf_ml_add_languages');